package com.squirrel.michale;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/2/2 9:06 PM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class Exam_iLearning20221021 {

    private int getApprovalTimes(int[] techs, DeptRelation[] relations, int myDeptId) {

        int dNum = techs.length;

        int[] tree = new int[dNum];

        for (int l = 0; l < dNum; l++) {

            tree[l] = -1;

        }

        for (DeptRelation d : relations) {

            int fat = d.deptId;

            for (int i = 0; i < d.children.length; i++) {

                tree[d.children[i]] = fat;

            }

        }


        Map<Integer, Integer> hm = new HashMap<>();

        hm.put(myDeptId, 0);

        int tmp = myDeptId;

        int level = 1;

        while (tree[tmp] != -1) {

            tmp = tree[tmp];

            hm.put(tmp, level);

            level++;

        }

        int res = 0;

        for (int i = 0; i < dNum; i++) {

            if((i != myDeptId) && (techs[i] == techs[myDeptId])) {

                int temp = i;

                while (!hm.keySet().contains(temp)) {

                    temp = tree[temp];

                }

                res = res + hm.get(temp);

            }

        }

        return res;

    }

    static class DeptRelation {

        int deptId; // 部门id

        int[] children; // 直接下级部门

        DeptRelation(int[] intArr) {

            this.deptId = intArr[0];

            if (intArr.length > 2) {

                this.children = Arrays.copyOfRange(intArr, 2, intArr.length);

            }

        }

        public String toString() {

            return String.format(Locale.ROOT, "%d %s",

                this.deptId, Arrays.toString(this.children));

        }

    }

 

    void dealInput(int[] techs, int[][] intArr2Dim, int myDeptId) {

        DeptRelation[] relations = new DeptRelation[intArr2Dim.length];

        for (int i = 0; i < intArr2Dim.length; i++) {

            relations[i] = new DeptRelation(intArr2Dim[i]);

        }

        int result = this.getApprovalTimes(techs, relations, myDeptId);

        System.out.println(result);

    }
}
